package com.topscore.uke.ui.activities;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TextInputEditText;
import android.support.design.widget.TextInputLayout;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.topscore.uke.R;
import com.topscore.uke.main.ATManager;
import com.topscore.uke.mvp.contractors.LoginContractor;
import com.topscore.uke.mvp.presenters.LoginPresenter;
import com.topscore.uke.ui.BaseCompatActivity;

import butterknife.Bind;
import butterknife.ButterKnife;

public class LoginActivity extends BaseCompatActivity implements LoginContractor.LoginView, View.OnClickListener {

    @Bind(R.id.phone_input_layout)
    TextInputLayout phoneInputLayout;
    @Bind(R.id.password_input_layout)
    TextInputLayout passwordInputLayout;
    @Bind(R.id.tool_bar)
    Toolbar toolBar;
    @Bind(R.id.account_edt)
    TextInputEditText accountEdt;
    @Bind(R.id.password_edt)
    TextInputEditText passwordEdt;
    @Bind(R.id.account_delete_btn)
    RelativeLayout accountDeleteBtn;
    @Bind(R.id.password_delete_btn)
    RelativeLayout passwordDeleteBtn;
    @Bind(R.id.login_layout)
    LinearLayout loginLayout;
    @Bind(R.id.login_btn)
    Button loginBtn;

    private LoginContractor.Presenter mLoginPresenter;

    AlertDialog mMemberBindAlertDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        setSupportActionBar(toolBar);
        mLoginPresenter = new LoginPresenter(this);
        initViews();
    }

    private void initViews() {
        accountEdt.clearFocus();
        passwordEdt.clearFocus();
        phoneInputLayout.setHint("账号");
        passwordInputLayout.setHint("密码");
        accountDeleteBtn.setOnClickListener(this);
        passwordDeleteBtn.setOnClickListener(this);
        loginBtn.setOnClickListener(this);
        accountEdt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable != null && editable.length() > 0) {
                    accountDeleteBtn.setVisibility(View.VISIBLE);
                } else {
                    accountDeleteBtn.setVisibility(View.GONE);
                }
            }
        });
        passwordEdt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable != null && editable.length() > 0) {
                    passwordDeleteBtn.setVisibility(View.VISIBLE);
                } else {
                    passwordDeleteBtn.setVisibility(View.GONE);
                }
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            ATManager.backExit(mContext);
        }
        return false;
    }

    @Override
    public void onLoading(String loadingMessage) {
        showLoadingDialog(loadingMessage);
    }

    @Override
    public void onLoadFinished() {
        dismissLoadingDialog();
    }

    @Override
    public void onFail(int code, String errorMessage) {
        new AlertDialog.Builder(mContext)
                .setTitle("登陆失败")
                .setMessage(errorMessage)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                    }
                }).create().show();
    }

    @Override
    public void onLoginSuccess() {
        Toast.makeText(mContext, "登陆成功", Toast.LENGTH_SHORT).show();
        startActivity(new Intent(mContext, MainCompatActivity.class));
        finish();
    }

    @Override
    public EditText getLoginEdt() {
        return accountEdt;
    }

    @Override
    public EditText getPwdEdt() {
        return passwordEdt;
    }

    @Override
    public void onMemberUnBound() {
        mMemberBindAlertDialog = new AlertDialog.Builder(mContext)
                .setTitle("提示")
                .setMessage("您还没有绑定会员，是否绑定？")
                .setNegativeButton("跳过", (dialogInterface, i) -> {
                    mMemberBindAlertDialog.dismiss();
                    onLoginSuccess();
                })
                .setPositiveButton("立即绑定", (dialogInterface, i) -> {
                    Intent intent = new Intent(mContext, MemberBindingActivity.class);
                    startActivityForResult(intent, 2001);
                    mMemberBindAlertDialog.dismiss();
                })
                .create();
        mMemberBindAlertDialog.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 2001) {
            onLoginSuccess();
        }
    }

    @Override
    public void setLoginClickable(boolean clickable) {
        loginBtn.setClickable(clickable);
        if (clickable) {
            loginBtn.setBackgroundResource(R.color.colorPrimary);
        } else {
            loginBtn.setBackgroundColor(Color.parseColor("#dcdcdc"));
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.account_delete_btn:
                accountEdt.setText("");
                break;
            case R.id.password_delete_btn:
                passwordEdt.setText("");
                break;
            case R.id.login_btn:
                mLoginPresenter.login();
                break;
        }
    }
}
